package com.base.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.library.R;


public class ItemMenuView extends RelativeLayout {
    private ImageView imgMore, imgIcon;
    private TextView tvName, tvValue;
    private View topLine, bottomLine;

    public ItemMenuView(Context context) {
        this(context, null);
    }

    public ItemMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initView(context, attrs);
    }

    public ItemMenuView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        initView(context, attrs);
    }

    public void initView(Context context, AttributeSet attrs) {
        if (!isInEditMode()) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.item_menu_view, this);
            imgMore = (ImageView) view.findViewById(R.id.img_more);
            imgIcon = (ImageView) view.findViewById(R.id.img_icon);
            tvName = (TextView) view.findViewById(R.id.tv_item_name);
            tvValue = (TextView) view.findViewById(R.id.tv_item_value);
            topLine = view.findViewById(R.id.top_line);
            bottomLine = view.findViewById(R.id.bottom_line);
            TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.ItemMenuView);
            tvName.setText(typeArray.getResourceId(R.styleable.ItemMenuView_name_text, R.string.str_empty));
            tvValue.setText(typeArray.getResourceId(R.styleable.ItemMenuView_value_text, R.string.str_empty));
            tvName.setTextColor(typeArray.getColor(R.styleable.ItemMenuView_name_text_color, context.getResources().getColor(R.color.main_edit_color)));
            tvValue.setHint(typeArray.getResourceId(R.styleable.ItemMenuView_hint_text, R.string.str_empty));
            imgIcon.setImageResource(typeArray.getResourceId(R.styleable.ItemMenuView_item_icon, 0));
            LayoutParams top = (LayoutParams) topLine.getLayoutParams();
            top.setMargins(typeArray.getDimensionPixelSize(R.styleable.ItemMenuView_top_line_left, R.dimen.main_margin_left_right), top.topMargin, top.rightMargin, top.bottomMargin);
            topLine.setLayoutParams(top);
            LayoutParams bottom = (LayoutParams) bottomLine.getLayoutParams();
            bottom.setMargins(typeArray.getDimensionPixelSize(R.styleable.ItemMenuView_bottom_line_left, R.dimen.main_margin_left_right), bottom.topMargin, bottom.rightMargin, bottom.bottomMargin);
            bottomLine.setLayoutParams(bottom);
            boolean more_visibility = typeArray.getBoolean(R.styleable.ItemMenuView_more_visible, true);
            boolean icon_visibility = typeArray.getBoolean(R.styleable.ItemMenuView_icon_visible, true);
            boolean top_visibility = typeArray.getBoolean(R.styleable.ItemMenuView_top_line_visible, false);
            boolean bottom_visibility = typeArray.getBoolean(R.styleable.ItemMenuView_bottom_line_visible, true);
            if (icon_visibility) {
                imgIcon.setVisibility(View.VISIBLE);
            } else {
                imgIcon.setVisibility(View.GONE);
            }
            if (more_visibility) {
                imgMore.setVisibility(View.VISIBLE);
            } else {
                imgMore.setVisibility(View.INVISIBLE);
            }
            if (top_visibility) {
                topLine.setVisibility(View.VISIBLE);
            } else {
                topLine.setVisibility(View.GONE);
            }
            if (bottom_visibility) {
                bottomLine.setVisibility(View.VISIBLE);
            } else {
                bottomLine.setVisibility(View.GONE);
            }
            typeArray.recycle();
        }
    }

    public void setText(String text) {
        tvValue.setText(text);
    }

    public void setName(String text) {
        tvName.setText(text);
    }

    public String getText() {
        return tvValue.getText().toString().trim();
    }

    public void setTextColor(int color) {
        tvName.setTextColor(color);
    }

    public void clear() {
        tvValue.setText(R.string.str_empty);
    }
}
